﻿using System.Windows;
using System.Windows.Controls;
using System;
using System.Linq;

namespace SecurePO
{
    /// <summary>
    /// Interaction logic for PurchaseOrderItem.xaml
    /// </summary>
    public partial class PurchaseOrderItem : UserControl
    {
        public PurchaseOrderItem()
        {
            InitializeComponent();
        }

        public string Title
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

        public string UserName
        {
            get;
            set;
        }

        public decimal Amount
        {
            get;
            set;
        }

        public DateTime Added
        {
            get;
            set;
        }

        public DateTime Approved
        {
            get;
            set;
        }

        public bool CanApprove
        {
            get;
            set;
        }

        public bool BioIdentityRequired
        {
            get;
            set;
        }

        public string Approver
        {
            get;
            set;
        }

        public bool IsNew
        {
            get;
            set;
        }

        public long Id
        {
            get;
            set;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            string titleFormat = "{0} - {1:C}";
            string noTitle = "<No Title>";
            string noDescription = "<No Description>";
            string userInformationFormat = "{0} {1:d/M/yyyy}";
            string noUserName = "<No User Name>";

            string title = !string.IsNullOrEmpty(this.Title) ? string.Format(titleFormat, this.Title, this.Amount) : string.Format(titleFormat, noTitle, this.Amount);
            string userInformation = !string.IsNullOrEmpty(this.UserName) ? string.Format(userInformationFormat, this.UserName, this.Added) : string.Format(userInformationFormat, noUserName, this.Added);

            this.TitleRun.Text = title;
            this.DescriptionRun.Text = !string.IsNullOrEmpty(this.Description) ? this.Description : noDescription;
            this.UserInformationRun.Text = userInformation;

            if (this.Approved.ToString() == DateTime.MaxValue.ToString())
            {
                if (this.BioIdentityRequired)
                {
                    ButtonText.Text = @"PO Approval Requires Fingerprint Scan";
                    ApprovalButton.Style = ((Style)FindResource("ButtonStyle_approveme"));
                }
                else
                {
                    ButtonText.Text = @"You May Approve this PO";
                    ApprovalButton.Style = ((Style)FindResource("button_approval_ready"));
                }

                if (!this.CanApprove)
                {
                    ButtonText.Text = string.Empty;
                    ApprovalButton.Visibility = Visibility.Hidden;
                }
            }
            else
            {
                string approverName = ActiveDirectoryHelper.GetUserNameBySid(this.Approver);
                ButtonText.Text = string.Format("Approved {0:dd MMMM yyyy} by {1}", this.Approved, approverName);
                ApprovalButton.Style = ((Style)FindResource("button_approved_confirmed"));
            }
        }

        public event EventHandler<PurchaseOrderItemEventArgs> ApprovalRequested;

        public event EventHandler<PurchaseOrderItemEventArgs> BioApprovalRequested;

        private void ApprovalButton_Click(object sender, RoutedEventArgs e)
        {
            PurchaseOrder po;
            using (SPOEntities entities = new SPOEntities())
            {
                po = entities.PurchaseOrder.Where(p => p.Id == this.Id).FirstOrDefault();
            }
            if (!this.BioIdentityRequired)
            {
                if (ApprovalRequested != null)
                {
                    if (po != null)
                    {
                        ApprovalRequested(this, new PurchaseOrderItemEventArgs(po));
                    }
                }
            }
            else
            {
                if (BioApprovalRequested != null)
                {
                    if (po != null)
                    {
                        BioApprovalRequested(this, new PurchaseOrderItemEventArgs(po));
                    }
                }
            }
        }
    }

    public class PurchaseOrderItemEventArgs : EventArgs
    {
        public PurchaseOrderItemEventArgs(PurchaseOrder po)
        {
            this.PO = po;
        }

        public PurchaseOrder PO
        {
            get;
            set;
        }
    }
}